#define ADRENALIN_EXPORTS

#include "../../include/DirectX11/DirectX11VertexShader.h"

namespace Adrenalin
{
	DirectX11VertexShader::DirectX11VertexShader()
		: _vertexShader(NULL)
	{}

	void DirectX11VertexShader::Close()
	{
		if(_vertexShader!=NULL)
			_vertexShader->Release();

		WRITELOG(L"Shader " + _file + L" closed", LOG_INFO);
	}

	EResult DirectX11VertexShader::LoadShader(Adrenalin::stringw& file, Adrenalin::string& function)
	{
		_file = file;
		_function = function;

		if(FAILED(D3DX11CompileFromFile(file.c_str(),0, 0, function.c_str(), "vs_5_0", 0, 0, 0, &_blob, 0, 0)))
		{
			return FAIL;
		}
		
		Adrenalin::DirectX11Device::Instance()->GetDevice()->CreateVertexShader(
			_blob->GetBufferPointer(), _blob->GetBufferSize(), NULL, &_vertexShader);

		WRITELOG(L"Shader " + _file + L" loaded", LOG_INFO);

		ShaderHandler::Instance()->AddShader(this);
		
		//TODO: Muss noch raus
		this->SetShader();

		return OK;
	}

	Adrenalin::stringw DirectX11VertexShader::GetFilename()
	{
		return _file;
	}
		
	Adrenalin::string  DirectX11VertexShader::GetFunction()
	{
		return _function;
	}

	void	DirectX11VertexShader::SetShader()
	{
		// set the shader objects
		Adrenalin::DirectX11Device::Instance()->GetDeviceContext()->VSSetShader(_vertexShader, 0, 0);

		// create the input layout object
		D3D11_INPUT_ELEMENT_DESC ied[] =
		{
			{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
			{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
		};

		Adrenalin::DirectX11Device::Instance()->GetDevice()->CreateInputLayout(ied, 2, _blob->GetBufferPointer(), _blob->GetBufferSize(), &_layout);
		Adrenalin::DirectX11Device::Instance()->GetDeviceContext()->IASetInputLayout(_layout);
	}
};